package com.jinlia.show.modules.wxapp.service;

import com.jinlia.show.common.core.utils.UserInfoUtils;
import com.jinlia.show.modules.base.model.form.MemberCardForm;
import com.jinlia.show.modules.base.system.entity.MemberCard;
import com.jinlia.show.modules.base.system.entity.MemberCardType;
import com.jinlia.show.modules.base.system.service.MemberCardMapperService;
import com.jinlia.show.modules.base.system.service.MemberCardTypeMapperService;
import com.jinlia.show.modules.base.system.service.PhoneMsgMapperService;
import com.jinlia.show.modules.base.wxapp.entity.User;
import com.jinlia.show.modules.base.wxapp.service.UserMapperService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Service
public class MemberCardService {
    @Autowired
    private MemberCardMapperService memberCardMapperService;
    @Autowired
    private MemberCardTypeMapperService memberCardTypeMapperService;
    @Autowired
    private UserMapperService userMapperService;

    @Autowired
    private PhoneMsgMapperService phoneMsgMapperService;
    @Transactional
    public MemberCard addMemberCard(MemberCardForm memberCard) {
        /**
         * 校验手机验证码
         */
        phoneMsgMapperService.checkVerifyCode(memberCard.getMobile(),UserInfoUtils.getUserId().toString(),memberCard.getCode());
        /**
         * 校验手机号唯一
         */
        MemberCard oneMemberCard = memberCardMapperService.lambdaQuery()
                .eq(MemberCard::getMobile, memberCard.getMobile())
                .one();
        if (oneMemberCard == null) {
            MemberCardType oneDefaultMemberCardType = memberCardTypeMapperService.lambdaQuery()
                    .eq(MemberCardType::getAsDefault, 1)
                    .one();
            memberCard.setTypeId(oneDefaultMemberCardType.getId());//设置默认会员id
            memberCard.setConsumedTimes(Integer.valueOf(0));//默认消费次数
            memberCard.setBalance(BigDecimal.ZERO);
            memberCard.setAvailableScore(0);
            memberCard.setTotalScore(0);
            memberCardMapperService.save(memberCard);
            //更新关联关系
            userMapperService.lambdaUpdate()
                    .set(User::getMemberCardId, memberCard.getId())
                    .set(User::getPhoneNumber, memberCard.getMobile())
                    .eq(User::getId, UserInfoUtils.getUserId())
                    .update();
        }else {
            //更新关联关系
            userMapperService.lambdaUpdate()
                    .set(User::getMemberCardId, oneMemberCard.getId())
                    .set(User::getPhoneNumber, memberCard.getMobile())
                    .set(User::getNickName, memberCard.getName())
                    .eq(User::getId, UserInfoUtils.getUserId())
                    .update();
        }

        return memberCard;
    }
}
